'use strict';

/**
 * Config for the router
 */
angular.module('app')
	.run(['$rootScope', '$state', '$stateParams', '$location', 'IGNOREPERMISSION',
		function ($rootScope, $state, $stateParams, $location, IGNOREPERMISSION) {
			$rootScope.$state = $state;
			$rootScope.$stateParams = $stateParams;
			
			//加入权限控制
			$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
				var currentUrl = toState.name;
				$rootScope.currentUrl = currentUrl;//记录当前页
				if (!fromState.name) return;
				var searchVal = currentUrl.replace(/\./g, '');
				if (IGNOREPERMISSION.IGNORELIST.indexOf(searchVal) < 0) {
					// $location.path('/access/login');
					// $state.go('access.login');
				}
			});
		}]
	).config(['$stateProvider', '$urlRouterProvider', '$locationProvider', 'JQ_CONFIG', 'MODULE_CONFIG',
		function ($stateProvider, $urlRouterProvider, $locationProvider, JQ_CONFIG, MODULE_CONFIG) {
			var layout = "/statics/tpl/app.html";
			$urlRouterProvider
				.otherwise('/access/login');
			$stateProvider
				.state('access', {
					url: '/access',
					template: '<div ui-view class="fade-in-right-big smooth"></div>'
				}).state('access.login', {
				url: '/login',
				templateUrl: '/statics/tpl/access/login.html',
				controller: 'LoginCtrl',
				resolve: load(['/statics/js/app/access/login.js'])
			}).state('access.sso', {
				url: '/sso',
				templateUrl: '/statics/tpl/access/sso.html',
				controller: 'ssoCtrl',
				resolve: load(['/statics/js/app/access/sso.js'])
			}).state('app', {
				abstract: true,
				url: '/app',
				templateUrl: layout,
				resolve: load(['/statics/js/directives/hide_detail.js'])
			}).state('app.graph', {
				url: '/graph',
				template: '<div ui-view class="fade-in-up"></div>',
				resolve: load(['dashboard', '/statics/libs/assets/echarts/china.js'])
			}).state('app.graph.dashboard', {
				url: '/dashboard',
				templateUrl: '/statics/tpl/graph/dashborad.html',
				resolve: load(['/statics/js/app/graph/dashborad.js'])
			}).state('app.graph.datareport', {
				url: '/datareport',
				templateUrl: '/statics/tpl/graph/datareport/main.html',
				resolve: load(['smart-table', '/statics/js/app/graph/datareport.js'])
			}).state('app.assets', {
				url: '/assets',
				template: '<div ui-view class="fade-in-up"></div>',
				resolve: load(['assetsLayout', 'smart-table', 'dashboard'])
			}).state('app.assets.datacenter', {
				url: '/datacenter',
				templateUrl: '/statics/tpl/assets/datacenter/tab.html',
				resolve: load(['iscroll', '/statics/js/app/assets/datacenter/tab.js', 'ui.select'])
			}).state('app.assets.resource', {
				url: '/resource',
				templateUrl: '/statics/tpl/assets/resource/tab.html',
				resolve: load(['/statics/js/app/assets/resource/data.js', '/statics/js/app/assets/resource/tab.js', '/statics/js/app/assets/infrastructure/server.js'])
			}).state('app.assets.addResource', {
				url: '/addResource',
				templateUrl: '/statics/tpl/assets/resource/add.html',
				resolve: load(['/statics/js/app/assets/resource/addResource.js', '/statics/js/app/config/vmware/vmAddNetworkModal.js'])
			}).state('app.assets.network', {
				url: '/network',
				templateUrl: '/statics/tpl/assets/infrastructure/network/tab.html',
				resolve: load(['/statics/js/app/assets/infrastructure/network.js', '/statics/js/app/assets/infrastructure/safety.js'])
			}).state('app.assets.datastore', {
				url: '/datastore',
				templateUrl: '/statics/tpl/assets/datastore/tab.html',
				resolve: load(['/statics/js/app/assets/storage/storage.js', 'smart-table', '/statics/js/app/assets/storage/platform.js'])
			}).state('app.assets.addserver', {
				url: '/addserver/:flag/:id',
				templateUrl: '/statics/tpl/assets/infrastructure/server/add.html',
				resolve: load(['/statics/js/app/assets/infrastructure/addserver.js'])
			}).state('app.assets.addnetwork', {
				url: '/addnetwork/:flag/:id',
				templateUrl: '/statics/tpl/assets/infrastructure/network/add.html',
				resolve: load(['/statics/js/app/assets/infrastructure/addnetwork.js'])
			}).state('app.assets.addsafety', {
				url: '/addsafety/:flag/:id',
				templateUrl: '/statics/tpl/assets/infrastructure/network/safety/add.html',
				resolve: load(['/statics/js/app/assets/infrastructure/addsafety.js'])
			}).state('app.assets.addstorage', {
				url: '/addstorage/:flag/:id',
				templateUrl: '/statics/tpl/assets/infrastructure/storage/add.html',
				resolve: load(['/statics/js/app/assets/infrastructure/addstorage.js'])
			})
				.state('app.sortware', {
					url: '/sortware',
					template: '<div ui-view class="fade-in-up"></div>',
					resolve: load(['codeMirror', 'echarts', '/statics/libs/assets/echarts/shine.js', '/statics/css/sortware.css'])
				})
				.state('app.sortware.repository', {
					url: '/repository',
					templateUrl: '/statics/tpl/sortware/openstack/list.html',
					resolve: load(['/statics/js/app/sortware/openstack/openstack.js',
						'/statics/js/app/adhibition/model/tpl_task.js'
					])
				})
				.state('app.sortware.repositorydetail', {
					url: '/repositorydetail/:id/:port',
					templateUrl: '/statics/tpl/sortware/openstack/detail.html',
					resolve: load(['/statics/js/app/sortware/openstack/openstackDetail.js'])
				})
				.state('app.sortware.dockerimage', {
					url: '/dockerimage',
					templateUrl: '/statics/tpl/sortware/warehouse/list.html',
					resolve: load(['easyui', '/statics/js/app/sortware/warehouse/warehouse.js',
						'/statics/js/app/adhibition/model/tpl_task.js'])
				})
				.state('app.sortware.dockerimagedetail', {
					url: '/dockerimagedetail/:id',
					templateUrl: '/statics/tpl/sortware/warehouse/detail.html',
					resolve: load(['/statics/js/app/sortware/warehouse/warehouseDetail.js'])
				})
				//部署
				.state('app.sortware.dockerimagetiondeploy', {
					url: '/dockerimagetiondeploy/:id',
					templateUrl: '/statics/tpl/sortware/warehouse/gridin.html',
					resolve: load(['ui.select', '/statics/js/app/sortware/warehouse/warehousegridin.js'])
				})
				//应用
				.state('app.adhibition', {
					url: '/adhibition',
					template: '<div ui-view class="fade-in-up"></div>',
					resolve: load(['/statics/css/sortware.css'])
				})
				.state('app.adhibition.model', {
					url: '/model',
					templateUrl: '/statics/tpl/adhibition/model/list.html',
					resolve: load(['/statics/js/app/adhibition/model/model.js', 'smart-table', 'modelController'])
				})
				.state('app.adhibition.modeldetail', {
					url: '/modeldetail/:id',
					templateUrl: '/statics/tpl/adhibition/model/detail.html',
					resolve: load(['/statics/js/app/adhibition/model/modelDetail.js', 'modelController'])
				})
				.state('app.adhibition.modelgridin', {
					url: '/modelgridin/:id',
					templateUrl: '/statics/tpl/adhibition/model/applyGridin.html',
					resolve: load(['/statics/js/app/adhibition/model/modelgridin.js', 'ui.select', 'modelController'])
				})
				.state('app.adhibition.apply', {
					url: '/apply',
					templateUrl: '/statics/tpl/adhibition/apply/list.html',
					resolve: load(['/statics/js/app/adhibition/apply/apply.js', 'echarts',
						'/statics/css/sortware.css', '/statics/js/app/adhibition/apply/apply_dir.js'])
				})
				.state('app.adhibition.applytiondetail', {
					url: '/applytiondetail/:id/:clusterId',
					templateUrl: '/statics/tpl/adhibition/apply/detail.html',
					resolve: load(['/statics/js/app/adhibition/apply/applyDetail.js',
						'smart-table', 'codeMirror', 'ui.select',
						'echarts', '/statics/libs/assets/echarts/shine.js',
						'/statics/js/app/adhibition/apply/apply_dir.js',
						'/statics/js/app/config/kubernetes/jquery.mousewheel-min.js',
						'/statics/js/app/config/kubernetes/jquery.terminal.min.js',
						'/statics/js/app/config/kubernetes/jquery.terminal.css',
						'/statics/css/user.css',
					])
				})
				//部署
				.state('app.adhibition.applytiondeploy', {
					url: '/applytiondeploy/:id',
					templateUrl: '/statics/tpl/adhibition/apply/gridin.html',
					resolve: load(['/statics/js/app/adhibition/apply/applygridin.js',
						, 'ui.select', 'modelController'])
				})
				//服务
				.state('app.service', {
					url: '/service',
					template: '<div ui-view class="fade-in-up"></div>',
					
				})
				.state('app.service.services', {
					url: '/services',
					templateUrl: '/statics/tpl/service/services/list.html',
					resolve: load(['/statics/js/app/service/services/model.js'])
				})
				.state('app.service.servicesdetail', {
					url: '/servicesdetail/:id',
					templateUrl: '/statics/tpl/service/services/detail.html',
					resolve: load(['/statics/js/app/service/services/servicesDetail.js'])
				})
				//监测
				.state('app.supervise', {
					url: '/supervise',
					template: '<div ui-view class="fade-in-up"></div>',
					
				})
				.state('app.supervise.apply', {
					url: '/apply',
					templateUrl: '/statics/tpl/supervise/apply/list.html',
					resolve: load(['/statics/js/app/supervise/apply/apply.js'])
				})
				.state('app.supervise.applydetail', {
					url: '/applydetail/:id',
					templateUrl: '/statics/tpl/supervise/apply/detail.html',
					resolve: load(['/statics/js/app/supervise/apply/applyDetail.js'])
				})
				//日志
				.state('app.journal', {
					url: '/journal',
					template: '<div ui-view class="fade-in-up"></div>',
					
				})
				.state('app.journal.apply', {
					url: '/apply',
					templateUrl: '/statics/tpl/journal/apply/list.html',
					resolve: load(['/statics/js/app/journal/apply/apply.js'])
				})
				.state('app.journal.applydetail', {
					url: '/applydetail/:id',
					templateUrl: '/statics/tpl/journal/apply/detail.html',
					resolve: load(['/statics/js/app/journal/apply/applyDetail.js'])
				})
				//配置
				.state('app.configuration', {
					url: '/configuration',
					template: '<div ui-view class="fade-in-up"></div>',
					
				})
				.state('app.configuration.dictionary', {
					url: '/dictionary',
					templateUrl: '/statics/tpl/configuration/dictionary/list.html',
					resolve: load(['/statics/js/app/configuration/dictionary/dictionary.js'])
				})
				.state('app.configuration.dictionarydetail', {
					url: '/dictionarydetail/:id',
					templateUrl: '/statics/tpl/configuration/dictionary/detail.html',
					resolve: load(['/statics/js/app/configuration/dictionary/dictionaryDetail.js'])
				})
				.state('app.configuration.strategy', {
					url: '/strategy',
					templateUrl: '/statics/tpl/configuration/strategy/list.html',
					resolve: load(['/statics/js/app/configuration/strategy/strategy.js'])
				})
				.state('app.configuration.strategytiondetail', {
					url: '/strategytiondetail/:id',
					templateUrl: '/statics/tpl/configuration/strategy/detail.html',
					resolve: load(['/statics/js/app/configuration/strategy/strategyDetail.js'])
				})
				.state('app.config', {
					url: '/config',
					template: '<div ui-view class="fade-in-up"></div>',
					resolve: load(['codeMirror', 'echarts', '/statics/libs/assets/echarts/shine.js', '/statics/js/app/config/vmware/vmAddNetworkModal.js'])
				}).state('app.config.vmwareTab', {
				url: '/vmwareTab',
				templateUrl: '/statics/tpl/config/vmware/tab.html',
				resolve: load(['iscroll', '/statics/js/app/config/vmware/vmTab.js', 'smart-table'])
			}).state('app.config.vmperformance', {
				url: '/vmperformance',
				templateUrl: '/statics/tpl/config/vmware/performance.html',
				resolve: load(['/statics/js/app/config/vmware/performance.js'])
			}).state('app.config.vmwareAdd', {
				url: '/vmwareAdd/:id',
				templateUrl: '/statics/tpl/config/vmware/vm/add.html',
				resolve: load(['/statics/js/app/config/vmware/vmAdd.js'])
			}).state('app.config.vmwareUpdate', {
				url: '/vmwareUpdate/:id',
				templateUrl: '/statics/tpl/config/vmware/vm/update.html',
				resolve: load(['/statics/js/app/config/vmware/vmUpdate.js'])
			}).state('app.config.vmwareTemplateAdd', {
				url: '/vmwareTemplateAdd/:id/:templateId',
				templateUrl: '/statics/tpl/config/vmware/vm/addTemplate.html',
				resolve: load(['/statics/js/app/config/vmware/addTemplate.js'])
			}).state('app.config.openstackTab', {
				url: '/openstackTab',
				templateUrl: '/statics/tpl/config/openstack/tab.html',
				resolve: load(['iscroll', '/statics/js/app/config/openstack/vmTab.js', 'smart-table'])
			}).state('app.config.openstackPerformance', {
				url: '/openstackPerformance',
				templateUrl: '/statics/tpl/config/openstack/performance.html',
				resolve: load(['/statics/js/app/config/openstack/performance.js'])
			}).state('app.config.openstackAdd', {
				url: '/openstackAdd/:id',
				templateUrl: '/statics/tpl/config/openstack/vm/add.html',
				resolve: load(['/statics/js/app/config/openstack/vmAdd.js'])
			}).state('app.config.openstackUpdate', {
				url: '/openstackUpdate/:id/:vmId',
				templateUrl: '/statics/tpl/config/openstack/vm/update.html',
				resolve: load(['/statics/js/app/config/openstack/vmUpdate.js'])
			}).state('app.config.openstackTemplateAdd', {
				url: '/openstackTemplateAdd/:id/:templateId',
				templateUrl: '/statics/tpl/config/openstack/addTemplate.html',
				resolve: load(['/statics/js/app/config/openstack/addTemplate.js'])
			}).state('app.config.kubernetes', {
				url: '/kubernetes',
				templateUrl: '/statics/tpl/config/kubernetes/list.html',
				resolve: load(['iscroll', '/statics/js/app/adhibition/apply/apply_dir.js',
					'/statics/js/app/config/kubernetes/jquery.mousewheel-min.js',
					'/statics/js/app/config/kubernetes/jquery.terminal.min.js',
					'/statics/js/app/config/kubernetes/jquery.terminal.css',
					'/statics/js/app/config/kubernetes/kubernetes.js',
					'smart-table',
					'/statics/css/sortware.css'])
			}).state('app.alarm', {
				url: '/alarm',
				template: '<div ui-view class="fade-in-up"></div>'
			}).state('app.alarm.manage', {
				url: '/manage',
				templateUrl: '/statics/tpl/alarm/alarm.html',
				resolve: load(['/statics/js/app/alarm/alarm.js'])
			}).state('app.userCenter', {
				url: '/userCenter',
				template: '<div ui-view class="fade-in-up"></div>',
				resolve: load(['easyui', 'tableGrid'])
			}).state('app.userCenter.user', {
				url: '/user',
				templateUrl: '/statics/tpl/userCenter/user/user.html',
				resolve: load(['/statics/js/app/userCenter/user.js'])
			}).state('app.userCenter.userResource', {
				url: '/userResource/:id',
				templateUrl: '/statics/tpl/userCenter/user/bindhost.html',
				resolve: load(['/statics/js/app/userCenter/userResource.js', 'smart-table'])
			}).state('app.userCenter.role', {
				url: '/role',
				templateUrl: '/statics/tpl/userCenter/role/role.html',
				resolve: load(['/statics/js/app/userCenter/role.js'])
			}).state('app.userCenter.permission', {
				url: '/permission',
				templateUrl: '/statics/tpl/userCenter/permission/permission.html',
				resolve: load(['/statics/js/app/userCenter/permission.js'])
			}).state('app.userCenter.department', {
				url: '/department',
				templateUrl: '/statics/tpl/userCenter/department/department.html',
				resolve: load(['/statics/js/app/userCenter/department.js'])
			}).state('app.userCenter.departmentResource', {
				url: '/departmentResource/:id',
				templateUrl: '/statics/tpl/userCenter/department/bindhost.html',
				resolve: load(['/statics/js/app/userCenter/departmentResource.js', 'smart-table'])
			}).state('app.userCenter.group', {
				url: '/group',
				templateUrl: '/statics/tpl/userCenter/group/group.html',
				resolve: load(['/statics/js/app/userCenter/group.js', 'smart-table'])
			}).state('app.userCenter.userGroupDetail', {
				url: '/userGroupDetail/:id',
				templateUrl: '/statics/tpl/userCenter/group/userGroup/detail.html',
				resolve: load(['/statics/js/app/userCenter/userGroupDetail.js', 'smart-table'])
			}).state('app.userCenter.resGroupDetail', {
				url: '/resGroupDetail/:id',
				templateUrl: '/statics/tpl/userCenter/group/resourceGroup/detail.html',
				resolve: load(['/statics/js/app/userCenter/resourceGroupDetail.js', 'smart-table'])
			}).state('app.userCenter.audit', {
				url: '/audit',
				templateUrl: '/statics/tpl/log/audit.html',
				resolve: load(['/statics/js/app/log/audit.js'])
			}).state('app.personal', {
				url: '/personal',
				template: '<div ui-view class="fade-in-up"></div>'
			}).state('app.personal.info', {
				url: '/info',
				templateUrl: '/statics/tpl/personal/info/index.html',
				resolve: load(['/statics/js/app/personal/info.js'])
			}).state('app.personal.approval', {
				url: '/approval',
				templateUrl: '/statics/tpl/personal/approval/index.html',
				resolve: load(['smart-table', '/statics/js/app/personal/approval.js'])
			}).state('app.personal.message', {
				url: '/message',
				templateUrl: '/statics/tpl/personal/message/index.html',
				resolve: load(['/statics/js/app/personal/message.js'])
			}).state('app.personal.applyservice', {
				url: '/applyservice',
				templateUrl: '/statics/tpl/personal/applyservice/index.html',
				resolve: load(['/statics/js/app/personal/apply_service.js'])
			}).state('app.personal.vmadd', {
				url: '/vmadd/:id/:type',
				templateUrl: '/statics/tpl/personal/applyservice/addVm.html',
				resolve: load(['/statics/js/app/personal/vmAdd.js'])
			})
			function load(srcs, callback) {
				return {
					deps: ['$ocLazyLoad', '$q',
						function ($ocLazyLoad, $q) {
							var deferred = $q.defer();
							var promise = false;
							srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/);
							if (!promise) {
								promise = deferred.promise;
							}
							angular.forEach(srcs, function (src) {
								//console.log(src);
								promise = promise.then(function () {
									if (JQ_CONFIG[src]) {
										return $ocLazyLoad.load(JQ_CONFIG[src]);
									}
									//if(src.substr(src.length-3, 3) === '.js') {
									//    name = src + '?v=' + OpsConstant.O_VERSION;
									//} else {
									name = src;
									//}
									return $ocLazyLoad.load(name);
								});
							});
							deferred.resolve();
							return callback ? promise.then(function () {
								return callback();
							}) : promise;
						}]
				}
			}
			
			// 去掉angular url的#号
			//$locationProvider.html5Mode(true);
		}
	]
);
